package com.wxy.feiteng.blog02.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
//拦截在控制层添加了@controller的类,里面有的异常
@ControllerAdvice
public class ExceptionControllerHandler {
//    日志工厂,获取到日志,在括号里面填写该对象,通过反射机制获取到对象
    private final Logger logger =LoggerFactory.getLogger(this.getClass());
//    表示处理异常的方法
    @ExceptionHandler(Exception.class)
    public ModelAndView execptionHandler(HttpServletRequest request ,Exception e) throws Exception {

        logger.error("Request URL : {} ,message: {} ,Exception : {} ",request.getRequestURI(),e,"自己定义的错误信息写到了日志文件里面去");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("url",request.getRequestURI());
        modelAndView.addObject("exception",e.getMessage());
        modelAndView.addObject("exceptioin02",e);
        modelAndView.setViewName("error/error");
        if(AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class)!=null){
            throw e;
        }


        return modelAndView;


    }
}
